import {
  applyMoveFile,
  deleteMyReceive,
  downloadApplyFiles,
  downloadMyReceive,
  fileapplyDelete,
  fileapplyList,
  fileapplyPreivew,
  getEditLinkPath,
  queryApplyDetail,
  queryDirTreeList,
  queryMyReceive,
  queryDatadictList,
} from '@/services/workspace';
import { decode, } from '@/utils/AesUtil';

export default {
  namespace: 'workspaceModel',

  state: {
    // 列表数据
    data: {
      dataList: [],
      pageNum: 0,
      pageSize: 0,
      totalCount: 0,
    },

    queryForm: {},
    detail: null,//详情
  },

  effects: {
    * fetch({ payload }, { call, put }) {
      const response = yield call(queryMyReceive, payload);
      if (response && response.result == 'ok') {

        yield put({
          type: 'save',
          payload: response,
        });
      }
    },

    * remove({ payload, callback }, { call, put }) {
      const response = yield call(deleteMyReceive, payload);
      if (response && response.result == 'ok') {

        yield put({
          type: 'save',
          payload: response,
        });
      }
      if (callback) callback();
    },
    //  查询申请列表
    * queryApplyPageList({ payload }, { call, put }) {
      const response = yield call(queryApplyPageList, payload);
      if (response && response.result == 'ok') {

        yield put({
          type: 'saveApplyList',
          payload: response,
        });
      }
    },
    //查询目录组织树
    * queryDirTreeList({ payload, callback }, { call, put }) {
      const response = yield call(queryDirTreeList, payload);
      if (response && response.result == 'ok') {

        yield put({
          type: 'saveDirTreeList',
          payload: response,
        });
      }
    },
    //在线编辑地址
    * getEditLinkPath({ payload, callback }, { call, put }) {
      const response = yield call(getEditLinkPath, payload);
      if (callback) callback(response);
    },

    //  查询申请详情
    * queryApplyDetail({ payload }, { call, put }) {
      const response = yield call(queryApplyDetail, payload);
      if (response && response.result == 'ok') {
        const data = response.data
        data.outVo && (data.outVo.filePwd = yield decode(data.outVo.filePwd))
        yield put({
          type: 'saveApplyDetail',
          payload: data,
        });
      }
    },
    //  个人文件下载
    * addPrivateFileDownloadApply({ payload }, { call, put }) {
      const response = yield call(queryApplyDetail, payload);
      if (response && response.result == 'ok') {

        yield put({
          type: 'saveApplyDetail',
          payload: response,
        });
      }
    },
    //我的申请
    * fileapplyList({ payload, callback }, { call, put }) {
      const response = yield call(fileapplyList, payload);
      if (response && response.result == 'ok') {

        yield put({
          type: 'saveApplyList',
          payload: response,
        });
        if (callback) callback(response);
      }
    },
    * fileapplyDelete({ payload, callback }, { call, put }) {
      const response = yield call(fileapplyDelete, payload);
      if (response && response.result == 'ok') {

        yield put({
          type: 'savefileapplyDelete',
          payload: response,
        });
        if (callback) callback(response);
      }
    },
    * moveFile({ payload, callback }, { call, put }) {
      const response = yield call(applyMoveFile, payload);
      if (response && response.result == 'ok') {

        yield put({
          type: 'savemoveFile',
          payload: response,
        });
        if (callback) callback(response);
      }
    },
    * downloadApplyFiles({ payload, callback }, { call, put }) {
      const response = yield call(downloadApplyFiles, payload);
      if (response && response.result == 'ok') {
        yield put({
          type: 'savedownloadApplyFiles',
          payload: response,
        });
      }
      if (callback) callback(response);
    },
    * fileapplyPreivew({ payload, callback }, { call, put }) {
      const response = yield call(fileapplyPreivew, payload);
      yield put({
        type: 'savefileapplyPreivew',
        payload: response,
      });
      if (callback) callback(response);
    },

    * queryDataDict({ payload, callback, }, { call, put }) {
      const response = yield call(queryDatadictList, payload);
      if (callback) callback(response);
    },

  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },

    saveApplyList(state, action) {
      return {
        ...state,
        data: action.payload.data,
      };
    },
    saveApplyDetail(state, action) {
      return {
        ...state,
        detail: action.payload
      };
    },
    saveDirTreeList(state, action) {
      return {
        ...state,
        dirTreeList: action.payload.data,
      };
    },


  },
};
